#!/bin/bash
mkdir -p $PWD/logs

###### const definition
img_mvn="maven:3.3.3-jdk-8"                 # docker image of maven
m2_cache=~/.m2                              # the local maven cache dir
proj_home=$PWD                              # the project root dir
img_output="foreveross/spring-cloud-config-server"         # output image tag

###### step1
git pull  # should use git clone https://name:pwd@xxx.git

###### step2 build project
if which mvn ; then
    echo "use local maven"
    mvn clean package -Dmaven.test.skip=true
else
    echo "use docker maven"
    docker run --rm \
        -v $m2_cache:/root/.m2 \
        -v $proj_home:/usr/src/mymaven \
        -w /usr/src/mymaven $img_mvn mvn clean package -Dmaven.test.skip=true
fi

###### step3 build image
sudo mv $proj_home/target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar $proj_home/target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar
docker build -t $img_output .

###### step4 run containers
docker rm -f springCloudConfigServer &> /dev/null
docker run -d --restart=on-failure:5 -v $PWD/logs:/home/logs --net=host --name  springCloudConfigServer ${img_output} java -Djava.security.egd=file:/dev/./urandom -jar /home/spring-cloud-config-server-0.0.1-SNAPSHOT.jar
